
const baseURL = process.env.BASE_URL
const { generateToken, parseToken } = require('../utils/token')
const { transToSql, transToText, insertImgUrl } = require('../utils/transChar');

// 包装 用户 数据
function returnUserData(userData) {
    const filePath = `${baseURL}/${userData.img}`
    const token = generateToken({ id: userData.id, username: userData.name })
    const { id, name: user, gender, email, registAt, lastAt, tel, address, bio, prefer } = userData
    return { id, user, img: filePath.replace(/\\/g, '/').replace(/public\//, ''), gender, email, registAt, lastAt, tel, address, bio, prefer, token }
}


// content 处理
function handleContent(content) {
    if (content) {
        const contentArr = content.split('&img').filter(item => item!== '')
        contentData = contentArr.map((item, index) => {
            if (item.startsWith('uploads\\contentImg')) {
                return { name: 'contentImg' + index, url: `${baseURL}/${item.replace(/\\/g, '/')}` }
            } else {
                return { name: 'contentText' + index, content: transToText(item).replace(/uploads\\/, '') }
            }
        });
        return contentData
    } else {
        return []
    }
}

// 包装 content 处理为 纯文本 的 文章显示
function returnCardText(content) {
    const text = content.map(con => {
        if (con.name.includes('contentText')) {
            return con.content
        }
        if (con.name.includes('contentImg')) {
            return '[图片]'
        }
    })
    return text.join('').substring(0, 200)
}


// 包装 work 数据
function returnWorkArticleData(workArticleData) {
    const { title, mainImg, content, authorId, } = workArticleData
    // 文本
    const contentData = handleContent(content)
    // content 处理为 content1 img1
    let filePath = ''
    if (mainImg) {
        filePath = `${baseURL}/${mainImg.replace(/\\/g, '/')}`
    }
    return { title, mainImg: filePath, content: contentData, authorId }
}

// 包装 article card 的数据
function returnCardData(ArticleData) {
    const { id, title, mainImg, content, authorId, views, createAt, tags, author: { name, img }, follow } = ArticleData
    const msg = returnCardText(handleContent(content))
    let filePath = ''
    if (mainImg) {
        filePath = `${baseURL}/${mainImg.replace(/\\/g, '/')}`
    }
    let userImg = ''
    if (img) {
        userImg = `${baseURL}/${img.replace(/\\/g, '/')}`
    }
    const tagArr = tags.map(tag => {
        return tag.name
    })
    return { 
        id, 
        title, 
        mainImg: filePath,
        content: msg, 
        authorId, 
        authorName: name, 
        authorImg: userImg,
        time: createAt, 
        tag: tagArr, 
        followed: follow.length,
        views, 
    }
}


// 包装 阅读时的文章显示
function returnReaderData(ArticleData, userId) {
    const { id, title, mainImg, content, authorId, views, createAt, tags, author, follow } = ArticleData
    const contentData = handleContent(content)
    let filePath = ''
    if (mainImg) {
        filePath = `${baseURL}/${mainImg.replace(/\\/g, '/')}`
    }
    let userImg = ''
    if (author.img) {
        userImg = `${baseURL}/${author.img.replace(/\\/g, '/')}`
    }
    const like = follow.some(user => user.userId === userId )
    return {
        id, 
        title, 
        mainImg: filePath,
        content: contentData, 
        time: createAt, 
        authorId: author.id, 
        authorName: author.name, 
        authorImg: userImg,
        isHeart: like
    }
    
}

//  专栏
function returnColumnHotData(columnsData) {
    const result = columnsData.map((column) => {
        let filePath = ''
        if (column.mainPath) {
            filePath = `${baseURL}/${column.mainPath.replace(/\\/g, '/')}`
        }
        return { 
            id: column.id, 
            name: column.name, 
            by: column.createUser.name, 
            img: filePath, 
            total: column.article.length,
            followed: column.follow.length,
        }
    })
    return result
}

function returnColumnMessage(column, userId) {
    const { id, name, mainPath, createAt, createUser, follow } = column
    let mainFile = ''
    if (mainPath) {
        mainFile = `${baseURL}/${mainPath.replace(/\\/g, '/')}`
    }
    let userImg = ''
    if (createUser.img) {
        userImg = `${baseURL}/${createUser.img.replace(/\\/g, '/')}`
    }
    const like = follow.some(column => column.userId === userId)
    return {
        id: id,
        name,
        img: mainFile,
        createdTime: createAt,
        createrId: createUser.id,
        createrName: createUser.name,
        createrImg: userImg,
        isCollected: like
    }
}


// edit 组件
// 包装 自己创建的专栏列表数据
function returnMyColumnsData(columnData) {
    const packs = columnData.map(item => {
        let mainImg = ''
        if (item.mainPath) {
            mainImg = `${baseURL}/${item.mainPath.replace(/\\/g, '/')}`
        }
        return { 
            columnId: item.id, 
            columnName: item.name, 
            columnImg: mainImg, 
            createrId: item.createId, 
            columnMsg: item.description 
        }
    })
    return packs
}
// 包装点击专栏列表后获取的 成员 数据
function returnMemberData(memberData) {
    const { member } = memberData
    // const {  } = memberData
    const result = member.map(item => {
        let filePath = ''
        if (item.user.img) {
            filePath = `${baseURL}/${item.user.img.replace(/\\/g, '/')}`
        }
        return {
            id: item.user.id, 
            name: item.user.name, 
            img: filePath
        }
    })
    return result
}

// 包装文章管理的数据
function returnMyAtricals(articlesData) {
    const result = articlesData.map(article => {
        let filePath = ''
        if (article.mainImg) {
            filePath = `${baseURL}/${article.mainImg.replace(/\\/g, '/')}`
        }
        return {
            id: article.id, 
            title: article.title, 
            dateTime: article.createAt,
            content: returnCardText( handleContent(article.content)),
            mainImg: filePath,
            published: article.published
        }
    })
    return result
}


// 评论相关
// 包装发送评论的返回值
function returnSendComment(commentData) {
    const { id, userId, content, sendTime } = commentData
    return {
        id,
        userId,
        msg: content,
        time: sendTime
    }
}

// 包装 评论数据列表
function returnCommentList(commentsData, currentId) {
    const result = commentsData.map(comment => {
        const { id, content, sendTime, user, commentAcc } = comment
        let filePath = ''
        if (user.img) {
            filePath = `${baseURL}/${user.img.replace(/\\/g, '/')}`
        }
        const acc = commentAcc.some(data => data.userId && data.userId === currentId)
        return { 
            id,
            msg: content,
            time: sendTime,
            userId: user.id,  
            name: user.name,  
            userImg: filePath,  
            isAcc: acc,
            accAll: commentAcc.length
        }
    })
    return result
}

// 包装 userinfo 整个页面数据
function returnUserInfo(userData, userId) {
    const { id, name, img, gender, email, prefer,bio, lastAt, registAt, tel, address, createColumn, atricals, followed, followig} = userData
    let userImg = ''
    if (img) {
        userImg = `${baseURL}/${img.replace(/\\/g, '/')}`
    }
    const user = {
        id: id,
        img: userImg,
        name,
        gender,
        bio,
        email,
        prefer,
        lastAt,
        registAt,
        tel,
        address
    }
    const columns = createColumn.map(column => {
        const {id, name, mainPath, article, follow } = column
        let columnImg = ''
        if (mainPath) {
            columnImg = `${baseURL}/${mainPath.replace(/\\/g, '/')}`
        }
        return {
            id,
            name,
            img: columnImg,
            followed: follow.length,
            total: article.length
        }
    })
    const articlesData = atricals.map(article => {
        let filePath = ''
        if (article.mainImg) {
            filePath = `${baseURL}/${article.mainImg.replace(/\\/g, '/')}`
        }
        return {
            id: article.id, 
            title: article.title, 
            dateTime: article.createAt,
            content: returnCardText( handleContent(article.content)),
            mainImg: filePath,
            published: article.published
        }
    }) 
    const follows = followed.some(user  => user.followedId === userId)
    return {
        user: user,
        columns: columns,
        total: {
            followedTotal: followed.length,
            followigTotal: followig.length,
            columnTotal: createColumn.length,
            articleTotal: atricals.length
        },
        articles: articlesData,
        isFollowed: { follow: follows },
    }
}


module.exports = {
    returnUserData,
    returnWorkArticleData,
    returnCardData,
    returnCardText,
    returnReaderData,

    returnColumnHotData,
    returnColumnMessage,

    returnMyColumnsData,
    returnMemberData,
    returnMyAtricals,

    returnSendComment,
    returnCommentList,

    returnUserInfo
}